using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using UnityEngine;

namespace AdvancedInspector
{
	[Serializable]
	[DebuggerDisplay("Count = {Count}")]
	[ComVisible(false)]
	public class UDictionary<TKey, TValue> : ICollection, IDictionary, IEnumerable, IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, ISerializable, IDeserializationCallback, ISerializationCallbackReceiver
	{
		[SerializeField]
		private List<TKey> keys = new List<TKey>();

		[SerializeField]
		private List<TValue> values = new List<TValue>();

		[NonSerialized]
		private Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>();

		ICollection IDictionary.Keys
		{
			get
			{
				return dictionary.Keys;
			}
		}

		ICollection IDictionary.Values
		{
			get
			{
				return dictionary.Values;
			}
		}

		object IDictionary.this[object key]
		{
			get
			{
				if (!(key is TKey))
				{
					return null;
				}
				return dictionary[(TKey)key];
			}
			set
			{
				if (key is TKey && (value is TValue || value == null))
				{
					dictionary[(TKey)key] = (TValue)value;
				}
			}
		}

		public bool IsFixedSize
		{
			get
			{
				return false;
			}
		}

		public ICollection<TKey> Keys
		{
			get
			{
				return dictionary.Keys;
			}
		}

		public ICollection<TValue> Values
		{
			get
			{
				return dictionary.Values;
			}
		}

		public TValue this[TKey key]
		{
			get
			{
				return dictionary[key];
			}
			set
			{
				dictionary[key] = value;
			}
		}

		public int Count
		{
			get
			{
				return dictionary.Count;
			}
		}

		public bool IsReadOnly
		{
			get
			{
				return false;
			}
		}

		public bool IsSynchronized
		{
			get
			{
				return false;
			}
		}

		public object SyncRoot
		{
			get
			{
				return null;
			}
		}

		void IDictionary.Add(object key, object value)
		{
			if (key is TKey && (value is TValue || value == null))
			{
				dictionary.Add((TKey)key, (TValue)value);
			}
		}

		bool IDictionary.Contains(object key)
		{
			if (!(key is TKey))
			{
				return false;
			}
			return dictionary.ContainsKey((TKey)key);
		}

		void IDictionary.Remove(object key)
		{
			if (key is TKey)
			{
				dictionary.Remove((TKey)key);
			}
		}

		IDictionaryEnumerator IDictionary.GetEnumerator()
		{
			return ((IDictionary)dictionary).GetEnumerator();
		}

		void ICollection.CopyTo(Array array, int index)
		{
		}

		void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
		{
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return dictionary.GetEnumerator();
		}

		public void OnAfterDeserialize()
		{
			dictionary.Clear();
			for (int i = 0; i < keys.Count; i++)
			{
				if (keys[i] != null && (!(keys[i] is UnityEngine.Object) || (bool)(UnityEngine.Object)(object)keys[i]))
				{
					dictionary.Add(keys[i], values[i]);
				}
			}
		}

		public void OnBeforeSerialize()
		{
			keys.Clear();
			values.Clear();
			foreach (KeyValuePair<TKey, TValue> item in dictionary)
			{
				if (item.Key != null && (!(item.Key is UnityEngine.Object) || (bool)(UnityEngine.Object)(object)item.Key))
				{
					keys.Add(item.Key);
					values.Add(item.Value);
				}
			}
		}

		public void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			dictionary.GetObjectData(info, context);
		}

		public void OnDeserialization(object sender)
		{
			dictionary.OnDeserialization(sender);
		}

		public void Add(TKey key, TValue value)
		{
			dictionary.Add(key, value);
		}

		public bool ContainsKey(TKey key)
		{
			return dictionary.ContainsKey(key);
		}

		public bool Remove(TKey key)
		{
			return dictionary.Remove(key);
		}

		public bool TryGetValue(TKey key, out TValue value)
		{
			return dictionary.TryGetValue(key, out value);
		}

		public void Add(KeyValuePair<TKey, TValue> item)
		{
			dictionary.Add(item.Key, item.Value);
		}

		public void Clear()
		{
			dictionary.Clear();
		}

		public bool Contains(KeyValuePair<TKey, TValue> item)
		{
			return dictionary.ContainsKey(item.Key) && dictionary[item.Key].Equals(item.Value);
		}

		public bool Remove(KeyValuePair<TKey, TValue> item)
		{
			return dictionary.Remove(item.Key);
		}

		public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
		{
			return dictionary.GetEnumerator();
		}
	}
}
